﻿namespace PrismEx.Fixtures
{
    using System;

    using Microsoft.Practices.Prism.Events;

    using PrismEx.Events;

    public class TestDomain<TEventType>
        where TEventType : CompositePresentationEvent<TEventType>, new()
    {
        private IEventAggregator _eventAggregator;

        private TEventType _ev;

        public TestDomain<TEventType> WithEventAggregator(IEventAggregator eventAggregator)
        {
            this._eventAggregator = eventAggregator;
            return this;
        }

        public TestDomain<TEventType> WithEvent(TEventType ev)
        {
            this._ev = ev;

            return this;
        }

        public TestDomain<TEventType> WithSubscription(Action<TEventType> subscriptionDelegate)
        {
            Action<TEventType> delegateWrapper = a =>
                {
                    subscriptionDelegate(a);
                    SubscriptionDelegateCallCount++;
                };


            _eventAggregator.Subscribe(delegateWrapper);

            return this;
        }

        public void Publish()
        {
            this._eventAggregator.Publish<TEventType>();
        }

        public void Publish(TEventType ev)
        {
            this._eventAggregator.Publish(ev);
        }

        public int SubscriptionDelegateCallCount { get; private set; }
    }
}
